package com.sondon.mayi.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sondon.mayi.fruit.R;
import com.sondon.mayi.util.DensityUtil;


public class MyMultipleTopBar extends RelativeLayout {

	private TextView left;
	private TextView right;
	private TextView tv_title;

	private TopBarClickListener topBarClickListener;
	private TopBarTitleClickListener titleClickListener;
	private String str_title;

	private RelativeLayout.LayoutParams leftLayoutParams;
	private RelativeLayout.LayoutParams rightLayoutParams;
	private RelativeLayout.LayoutParams titleLayoutParams;
	private RelativeLayout.LayoutParams newsLayoutParams;

	private final static int leftId = 1001;
	private final static int titleTvId = 2001;
	private final static int rightId = 3001;
	private final static int newsId = 4001;

	private Drawable drawableLeft;
	private Drawable drawableRight;
	private Drawable drawableTitle;
	
	private ImageView ImgNews;

	private String str_LeftBtn;
	private String str_RightBtn;

	private int leftBtnColor;
	private int rightBtnColor;
	private int titleTvColor;

	private float titleTextSize;
	private float titleDrawablePadding;

	private boolean have_title = true;
	private boolean have_left = true;
	private boolean have_right = true;
	private boolean have_title_drawable=false;

	public MyMultipleTopBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		// 从参数列表中获取参数
		// TypedArray实例是个属性的容器，context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合
		// 第二个参数为 为获取到值时的默认值
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.TopBar);

		this.str_title = ta.getString(R.styleable.TopBar_title);
		titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize,14);
//		this.titleTextSize =DensityUtil.sp2px(context, ss) ;
//		LogUtil.e("DP_PX", "titleTextSize  :"+titleTextSize);
		this.titleTvColor = ta.getColor(R.styleable.TopBar_titleTextColor, 0);
		this.drawableTitle = ta.getDrawable(R.styleable.TopBar_drawable_title);
		this.titleDrawablePadding=ta.getDimension(R.styleable.TopBar_drawable_title_padding, 10);
		
		this.str_LeftBtn = ta.getString(R.styleable.TopBar_leftText);
		this.drawableLeft = ta.getDrawable(R.styleable.TopBar_drawable_left);
		this.leftBtnColor = ta.getColor(R.styleable.TopBar_leftTextColor, 0);

		this.str_RightBtn = ta.getString(R.styleable.TopBar_rightText);
		this.drawableRight = ta.getDrawable(R.styleable.TopBar_drawable_right);
		this.rightBtnColor = ta.getColor(R.styleable.TopBar_rightTextColor, 0);

		this.have_left = ta.getBoolean(R.styleable.TopBar_have_left, true);
		this.have_right = ta.getBoolean(R.styleable.TopBar_have_right, true);
		this.have_title = ta.getBoolean(R.styleable.TopBar_have_title, true);
		this.have_title_drawable=ta.getBoolean(R.styleable.TopBar_have_title_drawable, false);

		ta.recycle();

		left = new TextView(context);
		right = new TextView(context);
		tv_title = new TextView(context);

		this.ImgNews=new ImageView(context);
		this.ImgNews.setScaleType(ScaleType.FIT_XY);
		this.ImgNews.setBackgroundResource(R.drawable.red_dot);
		
		left.setId(leftId);
		right.setId(rightId);
		tv_title.setId(titleTvId);
		ImgNews.setId(newsId);
		
		// 为组件配置布局参数
		leftLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		rightLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		
		titleLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

		leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
		leftLayoutParams.setMargins(DensityUtil.dip2px(context, 10), 0, 0, 0);// 左上右下
		leftLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);

		rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
		rightLayoutParams.setMargins(0, 0, DensityUtil.dip2px(context, 10), 0);// 左上右下
		rightLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);

		titleLayoutParams.setMargins(DensityUtil.dip2px(context, 10), 0, DensityUtil.dip2px(context, 10), 0);// 左上右下
		titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL|RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
		titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
		tv_title.setGravity(Gravity.CENTER);

		newsLayoutParams =new RelativeLayout.LayoutParams(DensityUtil.dip2px(context, 10),DensityUtil.dip2px(context, 10));
		newsLayoutParams.setMargins(0,DensityUtil.dip2px(context, 10), 0, 0);// 左上右下
		newsLayoutParams.addRule(RelativeLayout.ALIGN_RIGHT,leftId);
		
		//添加View到RelativeLayout
		if (have_title) {
			addView(tv_title, titleLayoutParams);
		}

		if (have_right) {
			addView(right, rightLayoutParams);
		}

		if (have_left) {
			addView(left, leftLayoutParams);
		}
		
		
		
		left.setText(str_LeftBtn);
		//这一步必须要做,否则不会显示图片.
		drawableLeft.setBounds(0, 0, drawableLeft.getMinimumWidth(),drawableLeft.getMinimumHeight());
		left.setCompoundDrawables(drawableLeft, null, null, null);
		left.setTextColor(leftBtnColor);

		
		right.setText(str_RightBtn);
		drawableRight.setBounds(0, 0,drawableRight.getMinimumWidth(),drawableRight.getMinimumHeight());
		right.setCompoundDrawables(drawableRight,null,null, null);
		right.setTextColor(rightBtnColor);

		tv_title.setEllipsize(TruncateAt.MIDDLE);
		tv_title.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
		tv_title.setSingleLine(true);
		tv_title.setText(str_title);
		if(have_title_drawable){
			drawableTitle.setBounds(0, 0, drawableTitle.getMinimumWidth(),drawableTitle.getMinimumHeight());
			tv_title.setCompoundDrawables(null, null,drawableTitle, null);
			
		}
		tv_title.setTextSize(titleTextSize);
		tv_title.setTextColor(titleTvColor);

		
		left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (topBarClickListener != null) {
					topBarClickListener.leftBtnClick();
				}
			}
		});

		right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (topBarClickListener != null) {
					topBarClickListener.rightBtnClick();
				}
			}
		});
		
		tv_title.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if (titleClickListener != null) {
					titleClickListener.titleClick();
				}
			}
		});
		
	}

	/*
	 * 左右单击监听事件
	 */
	public void setTopBarClickListener(TopBarClickListener topBarClickListener) {
		this.topBarClickListener = topBarClickListener;
	}
	
	/*
	 * title单击监听事件
	 */
	public void setTopBarTitleClickListener(TopBarTitleClickListener titleClickListener) {
		this.titleClickListener = titleClickListener;
	}
	
	
	public void setTopBarTitleText(String str_title){
		tv_title.setText(str_title);
	}
	
	public void setNoticeNews(boolean have_news){
		if (have_news) {
			addView(ImgNews,newsLayoutParams);
		}else{
			removeView(ImgNews);
		}
	}
}